#coding: utf8
'''
==================================================================
Created on 2021年12月13日 By Simon

==================================================================
'''

from src.libs.database import Database
from src.libs.utils import TM
from packages.redis._compat import select

def change_name(user, t_id, name, new_name):
    db = Database()
    sql1, param1 = "update task set name=%s where id=%s", (name, t_id)
    sql2, param2 = "insert into operation (user, category, cid, src, dest) \
        values (%s, %s, %s, %s, %s)", (user, 'changeTaskName', t_id, name, new_name)
    flag, _ = db.Transaction([[sql1, param1], [sql2, param2]])
    
    return flag, "操作失败"


'''
计划变更后，实际最新进度会同步至task表，同步记录更新时间pTime。
故查询任务列表时可通过pTime比较获取真实进度
update task set progress=0, pTime=null where id=1276;
delete from plan_change where taskId=1276
'''
def change_plan(user, t_id, src_time, new_time, src_progress, progress, reason, 
                multi_user):
    db = Database()
    sql1, param1 = "update task set planEndTime=%s, progress=%s, pTime=%s where id=%s", \
        (new_time, progress, TM(), t_id)
    sql2, param2 = "insert into plan_change (taskId, srcTime, planTime, changeUser, reason, \
        preProgress, progress, multiUser) values (%s, %s, %s, %s, %s, %s, %s, %s)", \
        (t_id, src_time, new_time, user, reason, src_progress, progress, multi_user)
    sql3 = "insert into operation (user, category, cid, src, dest, description, mark) \
        values (%s, %s, %s, %s, %s, %s, %s)" 
    mark = "原进度：%s,修正后进度：%s" % (src_progress, progress)
    param3 = (user, 'changePlan', t_id, src_time, new_time, reason, mark)
    
    flag, _ = db.Transaction([[sql1, param1], [sql2, param2], [sql3, param3]])
    
    return flag, "操作失败"
    
    
'''
TODO：重新打开任务
select * from task where id = 1276
select * from record_day where taskId = 1276
update task set status = 0 where id = 1276;
update record_day set taskStatus = 0 where taskId = 1276
'''
def close_task(user, t_id, reason, progress):
    db = Database()
    sql1, param1 = "update task set progress=%s, status=%s where id=%s", (100, -1, t_id)
    sql2, param2 = "update record_day set taskStatus=%s where taskId=%s", (-1, t_id)
    sql3 = "insert into operation (user, category, cid, src, dest, description) \
        values (%s, %s, %s, %s, %s, %s)" 
    param3 = (user, 'closeTask', t_id, progress, 100, reason)
    flag, _ = db.Transaction([[sql1, param1], [sql2, param2], [sql3, param3]])
    
    return flag, "操作失败"


def finish_task(user, t_id, real_time, mark, progress):
    db = Database()
    sql1, param1 = "update task set progress=%s, status=%s where id=%s", (100, 1, t_id)
    sql2, param2 = "update record_day set taskStatus=%s where taskId=%s", (1, t_id)
    sql3 = "insert into operation (user, category, cid, src, dest, description, mark) \
        values (%s, %s, %s, %s, %s, %s, %s)"
    param3 = (user, 'finishTask', t_id, progress, 100, "完成时间：%s" % real_time, mark)
    flag, _ = db.Transaction([[sql1, param1], [sql2, param2], [sql3, param3]])
    
    return flag, "操作失败"

'''
1）普通用户仅能删除未提交过的任务；
2）暂未禁止用户删除他人创建的任务；
'''
def del_task(user, t_id):
    db = Database()
    sql = "select level, ct1, ct2 from (select level from user where username=%s) a \
        left join (select count(1) ct1 from plan_day where taskId=%s) b on 1\
        left join (select count(1) ct2 from record_day where taskId=%s) c on 1"
    info = db.selectLine(sql, (user, t_id, t_id))
    if info == None: return False, "用户权限验证失败"
    if (info[1] > 0 or info[2] > 0) and int(info[1]) < 1: return False, "无法删除已被提交过的任务"
    
    sql1, param1 = "delete from task where id=%s", (t_id, )
    sql2 = "delete from record_day where taskId=%s"
    sql3 = "delete from plan_day where taskId=%s"
    sql4 = "insert into operation (user, category, cid, src, dest, description) \
        values (%s, %s, %s, %s, %s, %s)"
    param4 = (user, 'delTask', t_id, info[0], info[1], "相关记录条数：%s" % info[0])
    flag, _ = db.Transaction([[sql1, param1], [sql2, param1], [sql3, param1],
                             [sql4, param4]])
    return flag, "操作失败"



if __name__ == '__main__':
    del_task("赵培", 11276)
    pass

